<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Exist4Array
 *
 * @author Moon
 */
class Exist4Array extends CExistValidator {
    //put your code here

    public $errorAttribute = '';
    protected function validateAttribute($object,$attribute) {
        $value=$object->$attribute;
        if($this->allowEmpty && $this->isEmpty($value))
            return;
        if(!is_array($value))
            $value = array($value);
        foreach($value as $i=>$_v) {
            $className=$this->className===null?get_class($object):Yii::import($this->className);
		$attributeName=$this->attributeName===null?$attribute:$this->attributeName;
		$finder=CActiveRecord::model($className);
		$table=$finder->getTableSchema();
		if(($column=$table->getColumn($attributeName))===null)
			throw new CException(Yii::t('yii','Column "{column}" does not exist in table "{table}".',
				array('{column}'=>$attributeName,'{table}'=>$table->name)));

		$criteria=array('condition'=>$column->rawName.'=:vp','params'=>array(':vp'=>$_v));
		if($this->criteria!==array())
		{
			$criteria=new CDbCriteria($criteria);
			$criteria->mergeWith($this->criteria);
		}
		if(!$finder->exists($criteria))
		{
                        if(!empty($this->errorAttribute))
                            $attribute = $this->errorAttribute;
			$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} "{value}" is invalid.');
			$this->addError($object,$attribute."[$i]",$message,array('{value}'=>$_v));
		}
        }
    }
}
?>
